import 'package:flutter/material.dart';
import 'package:top_foundation/utils/util.dart';

import '../../../utils/theme/theme.dart';
import '../index.dart';

/// hello
class MonthView extends GetView<CalendarViewController> {
  const MonthView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var state = controller.state;
    var itemW = (Get.width / 7).floor().toDouble();
    return Obx(
      () => Column(
        children: [
          Row(
            children: List.generate(7, (index) => getCurrWeekDay(index))
                .map((e) => Container(
                      width: itemW,
                      height: 30.r,
                      alignment: Alignment.center,
                      child: Text(
                        e,
                        style: TextStyle(
                          color: const Color(0xff333333),
                          fontSize: 16.sp,
                        ),
                      ),
                    ))
                .toList(),
          ),
          SizedBox(
            width: Get.width,
            child: Wrap(
                spacing: 0,
                children: getDays(state.curr.value.month)
                    .asMap()
                    .map(
                      (key, e) {
                        return MapEntry(
                          key,
                          InkWell(
                            onTap: () => controller.actionChoose(e),
                            child: Container(
                              width: itemW,
                              height: 30.r,
                              margin: EdgeInsets.symmetric(vertical: 5.r),
                              alignment: Alignment.center,
                              decoration: (!isInRangeAndLeftEdge(key, e) &&
                                      !isInRangeAndRightEdge(key, e) &&
                                      isInRange(e))
                                  ? const BoxDecoration(
                                      color: Color(0xffE2EEF9))
                                  : null,
                              child: Stack(
                                children: [
                                  getBgView(itemW, key, e),
                                  Center(
                                    child: Container(
                                      width: 30.r,
                                      height: 30.r,
                                      decoration: getBoxDecoration(key, e),
                                      alignment: Alignment.center,
                                      child: Stack(
                                        children: [
                                          Container(
                                            padding: const EdgeInsets.all(2),
                                            child: Text(
                                              e.month.day
                                                  .toString()
                                                  .padLeft(2, '0'),
                                              style: TextStyle(
                                                color: getTextColor(e),
                                              ),
                                            ),
                                          ),
                                          state.now.equal(e)
                                              ? Positioned.fill(
                                                  bottom: 0,
                                                  child: Row(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .center,
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment.end,
                                                    children: [
                                                      Container(
                                                        width: 4,
                                                        height: 4,
                                                        decoration:
                                                            BoxDecoration(
                                                          color: ITheme
                                                              .selectedColor,
                                                          borderRadius:
                                                              BorderRadius
                                                                  .circular(2),
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                )
                                              : const SizedBox()
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        );
                      },
                    )
                    .values
                    .toList()),
          ),
        ],
      ),
    );
  }

  bool isInRangeAndLeftEdge(int idx, DateModel e) {
    if (isInRange(e)) {
      if (idx % 7 == 0) {
        return true;
      }
    }
    return false;
  }

  bool isInRangeAndRightEdge(int idx, DateModel e) {
    if (isInRange(e)) {
      if ((idx + 1) % 7 == 0) {
        return true;
      }
    }
    return false;
  }

  Widget getBgView(double itemW, int idx, DateModel e) {
    if (isAtStart(e) || isAtEnd(e)) {
      return Positioned(
        left: isAtStart(e) ? itemW / 2 : 0,
        right: isAtEnd(e) ? itemW / 2 : 0,
        // width: itemW / 2,
        height: 30.r,
        child: Container(color: const Color(0xffE2EEF9)),
      );
    }
    if (isInRangeAndLeftEdge(idx, e)) {
      return Positioned(
        left: itemW / 2,
        width: itemW / 2,
        height: 30.r,
        child: Container(color: const Color(0xffE2EEF9)),
      );
    }

    if (isInRangeAndRightEdge(idx, e)) {
      return Positioned(
        right: itemW / 2,
        width: itemW / 2,
        height: 30.r,
        child: Container(color: const Color(0xffE2EEF9)),
      );
    }

    return const SizedBox();
  }

  BoxDecoration? getBoxDecoration(int idx, DateModel e) {
    if (isInRangeAndLeftEdge(idx, e)) {
      return BoxDecoration(
          color: const Color(0xffE2EEF9),
          borderRadius: BorderRadius.horizontal(left: Radius.circular(15.r)));
    }
    if (isInRangeAndRightEdge(idx, e)) {
      return BoxDecoration(
          color: const Color(0xffE2EEF9),
          borderRadius: BorderRadius.horizontal(right: Radius.circular(15.r)));
    }

    if ((e.equal(controller.state.start.value) ||
        e.equal(controller.state.end.value))) {
      return BoxDecoration(
          color: Colors.blue, borderRadius: BorderRadius.circular(15.r));
    }
    return null;
  }

  String getCurrWeekDay(int index) {
    switch (index) {
      case 0:
        return '日';
      case 1:
        return '一';
      case 2:
        return '二';
      case 3:
        return '三';
      case 4:
        return '四';
      case 5:
        return '五';
      case 6:
        return '六';
      default:
        return '';
    }
  }

  List<DateModel> getDays(MonthModel md) {
    var curr = controller.state.curr.value;
    var days = List.generate(md.days, (index) => index + 1)
        .map((e) => DateModel(
            year: curr.year,
            months: curr.months,
            month: MonthModel(
                month: curr.month.month, day: e, days: curr.month.days)))
        .toList();
    var prefixDays =
        List.generate(md.startWeekday % 7, (index) => md.lastMonthDays - index)
            .reversed
            .map((e) => DateModel(
                year: curr.lastYear,
                months: curr.months,
                month: MonthModel(
                    month: curr.month.lastMonth,
                    day: e,
                    days: curr.month.days)));
    var suffixDays = List.generate(6 - md.endWeekday % 7, (index) => index + 1)
        .map((e) => DateModel(
            year: curr.nextYear,
            months: curr.months,
            month: MonthModel(
                month: curr.month.nextMonth, day: e, days: curr.month.days)));
    days.insertAll(0, prefixDays);
    days.addAll(suffixDays);
    md.prefixDays = prefixDays.length;
    md.suffixDays = suffixDays.length;
    return days;
  }

  Color getTextColor(DateModel e) {
    var curr = controller.state.curr.value.month;
    var start = controller.state.start.value;
    var end = controller.state.end.value;
    if (start.equal(e) || end.equal(e)) {
      return Colors.white;
    }
    if (e.month.month != curr.month) {
      return const Color(0xff999999);
    }
    return const Color(0xff333333);
  }

  bool isInRange(DateModel e) {
    var start = controller.state.start.value;
    var end = controller.state.end.value;

    return start.isBefore(e) && end.isAfter(e);
  }

  bool isAtStart(DateModel e) {
    var start = controller.state.start.value;
    var end = controller.state.end.value;
    if (start.month.day != 0 && end.month.day != 0) {
      return start.equal(e);
    }
    return false;
  }

  bool isAtEnd(DateModel e) {
    var start = controller.state.start.value;
    var end = controller.state.end.value;
    if (start.month.day != 0 && end.month.day != 0) {
      return end.equal(e);
    }
    return false;
  }
}
